#include <iostream>
#include <string>
#include <signal.h>
#include "control.hpp"
#include "../httplib/cpp-httplib-v0.7.15/httplib.h"

using namespace std;
using namespace httplib;
using namespace ns_control;

Control* ctrl_ptr = nullptr;
void RECOVERY(int signo) //上线功能
{
    ctrl_ptr->Online();
}

//ojserver要完成：
int main()
{
    
    Server svr;
    Control ctrl;

    ctrl_ptr = &ctrl;
    signal(SIGQUIT, RECOVERY);

    svr.Get("/all_questions", [&ctrl](const Request& req, Response &resp){
        string out_html;
        ctrl.GetQuestionList(out_html);

        resp.set_content(out_html, "text/html; charset=utf-8");
    });

    //R"()"
    svr.Get(R"(/question/(\d+))", [&ctrl](const Request& req, Response &resp){
        string num = req.matches[1];
        string out_html;

        ctrl.GetOneQuestion(num, out_html);
        resp.set_content(out_html, "text/html; charset=utf-8");
    });

    svr.Post(R"(/judge/(\d+))", [&ctrl](const Request& req, Response &resp){
        string num = req.matches[1];
        string in_json = req.body;
        string out_json;

        ctrl.Judge(num, in_json, out_json);
        cout << out_json << endl;
        resp.set_content(out_json, "application/json; charset=utf-8");
    });

    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0", 8080);

    return 0;
}